package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"k8s-platform/service"
	"log"
	"net/http"
)

var Deployment deployment

type deployment struct{}

//获取pod列表  支持过滤  分页  排序
func (d *deployment) Getdeployments(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		FilterName string `form:"filter_name"`
		Namespace  string `form:"namespace"`
		Limit      int    `form:"limit"`
		Page       int    `form:"page"`
	})
	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.Bind(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}
	data, err := service.Deployment.GetDeployments(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "get deployment list success",
		"data": data,
	})
}

//获取deployment详情
func (d *deployment) GetDeployDetail(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		DeployName string `form:"deploy_name" binding:"required"`
		Namespace  string `form:"namespace" binding:"required"`
	})
	//form使用Bind方法  json使用ShouldBindJSON
	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.Bind(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	data, err := service.Deployment.GetDeployDetail(params.DeployName, params.Namespace)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "get deploy detail success",
		"data": data,
	})
}

//修改deploy副本数
func (d *deployment) UpdateDeployScale(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		DeployName string `json:"deploy_name" binding:"required"`
		Namespace  string `json:"namespace" binding:"required"`
		ScaleNum   int32  `json:"scale_num" binding:"required"`
	})

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	data, err := service.Deployment.ScaleDeployment(params.DeployName, params.Namespace,params.ScaleNum)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "update deploy scale success",
		"data": data,
	})
}

//创建deployment
/*
{
    "name":"ceshideapi",
    "namespace":"default",
	"replicas":2,
	"image":"harbor.ev.com:8000/rancher/busybox:latest",
    "label":{"aaa":"bbbb"},
    "cpu":"1",
	"memory":"200",
	"container_port":80,
	"health_check":false
}
*/
func (d *deployment) CreateDeployment(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := &service.DeployCreate {}

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	err := service.Deployment.CreateDeployment(params)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "create deployment success",
		"data": nil,
	})
}

//删除deployment
//{ "deploy_name":"ceshideapi", "namespace":"default" }
func (d *deployment)DeleteDeployment(c *gin.Context)  {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		DeployName string `json:"deploy_name" binding:"required"`
		Namespace  string `json:"namespace" binding:"required"`
	})

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	err := service.Deployment.DeleteDeploy(params.DeployName, params.Namespace)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "delete deployment success",
		"data": nil,
	})
}


//重启deployment
//{ "deploy_name":"ceshideapi", "namespace":"default" }
func (d *deployment)RestartDeployment(c *gin.Context)  {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		DeployName string `json:"deploy_name" binding:"required"`
		Namespace  string `json:"namespace" binding:"required"`
	})

	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}

	err := service.Deployment.ReStartDeploy(params.DeployName, params.Namespace)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "restart deployment success",
		"data": nil,
	})
}


//获取每个namespace中的deploy数量
func (d *deployment) GetDeployNum(c *gin.Context) {

	data, err := service.Deployment.GetDeployNumPerNp()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "get deployments num in namepase success",
		"data": data,
	})
}


//更新deployment
func (d *deployment) UpdateDeply(c *gin.Context) {
	//处理入参 匿名结构体 用于定义入参 get请求为form格式 其他请求为json
	params := new(struct {
		Content   string `json:"content"  binding:"required"`
		Namespace string `json:"namespace"  binding:"required"`
	})
	//form使用Bind方法  json使用ShouldBindJSON
	if err := c.ShouldBindJSON(params); err != nil {
		log.Printf("Bind绑定参数失败：%s", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  fmt.Sprintf("Bind绑定参数失败：%s", err),
			"data": nil,
		})
		return
	}
	err := service.Deployment.UpDateDeploy(params.Namespace,params.Content)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "update deploy success",
		"data": nil,
	})
}
